﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Gta.Common;
using Yoozoo.Managers;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Gameplay.PVE.Survivor
{
    public partial class BattleConfigManager: BattleSingleton<BattleConfigManager>
    {
        private Dictionary<int,BehaviorData> behaviorMap = new Dictionary<int, BehaviorData>();
        
        public void Initialize()
        {
            ClassManager.Store<Selector>(5);
            ClassManager.Store<Sequence>(5);
            ClassManager.Store<Inverter>(5);
            ClassManager.Store<NeedReload>(5);
            ClassManager.Store<IsReloading>(5);
            ClassManager.Store<Reload>(5);
            ClassManager.Store<HasTarget>(5);
            ClassManager.Store<IsShooting>(5);
            ClassManager.Store<StartShoot>(5);
            ClassManager.Store<UpdateShoot>(5);
            ClassManager.Store<SearchTarget>(5);
            ClassManager.Store<IsInSkill>(5);
            ClassManager.Store<UpdateSkill>(5);
            ClassManager.Store<IsSkillReady>(5);
            ClassManager.Store<AutoCastSkill>(5);
            ClassManager.Store<IsTargetInRange>(5);
            ClassManager.Store<CanMoveToTarget>(5);
            ClassManager.Store<MoveToTarget>(5);
            ClassManager.Store<StopShoot>(5);
            ClassManager.Store<IsTargetOutOfMaxRange>(5);
            behaviorMap.TryAdd(1,GetDefaultBehaviorData());
            behaviorMap.TryAdd(2,GetBasicBehaviorData());
        }
        
        public BehaviorData GetBehaviorData(int id)
        {
            return behaviorMap.GetValueOrDefault(id);
        }
        
        public static BehaviorData GetBasicBehaviorData()
        {
            var data = new BehaviorData();
            data.rootId = 1;
            data.composites = new Dictionary<int, int[]>();
            data.decorators = new Dictionary<int, int>();
            data.sharedVariables = new Dictionary<string, object>();

            data.composites.Add(1,new []{2,5,8,13,21});
            data.composites.Add(2,new []{3,4});
            data.composites.Add(5,new []{6,7});
            data.composites.Add(8,new []{9,10});
            data.composites.Add(10,new []{11,12});
            data.composites.Add(13,new []{33,24,16,19});
            data.composites.Add(33,new []{14,20});
            data.composites.Add(24,new []{15,25});
            data.composites.Add(25,new []{26,27});
            data.composites.Add(29,new []{30,31});
            data.composites.Add(16,new []{17,18});
            data.composites.Add(21,new []{22,28,23});
            data.decorators.Add(31,32);
            data.tasks = new[]
            {
                new BehaviorData.TaskData
                {
                    id = 1,
                    taskType = 2,
                    className = "Selector"
                },
                new BehaviorData.TaskData
                {
                    id = 2,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 3,
                    taskType = 1,
                    className = "IsInSkill"
                },
                new BehaviorData.TaskData
                {
                    id = 4,
                    taskType = 1,
                    className = "UpdateSkill"
                },
                new BehaviorData.TaskData
                {
                    id = 5,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 6,
                    taskType = 1,
                    className = "IsSkillReady"
                },
                new BehaviorData.TaskData
                {
                    id = 7,
                    taskType = 1,
                    className = "AutoCastSkill"
                },
                new BehaviorData.TaskData
                {
                    id = 8,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 9,
                    taskType = 1,
                    className = "NeedReload"
                },
                new BehaviorData.TaskData
                {
                    id = 10,
                    taskType = 2,
                    className = "Selector"
                },
                new BehaviorData.TaskData
                {
                    id = 11,
                    taskType = 1,
                    className = "IsReloading"
                },
                new BehaviorData.TaskData
                {
                    id = 12,
                    taskType = 1,
                    className = "Reload"
                },
                new BehaviorData.TaskData
                {
                    id = 13,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 14,
                    taskType = 1,
                    className = "HasTarget"
                },
                new BehaviorData.TaskData
                {
                    id = 15,
                    taskType = 1,
                    className = "IsTargetInRange"
                },
                new BehaviorData.TaskData
                {
                    id = 16,
                    taskType = 2,
                    className = "Selector"
                },
                new BehaviorData.TaskData
                {
                    id = 17,
                    taskType = 1,
                    className = "IsShooting"
                },
                new BehaviorData.TaskData
                {
                    id = 18,
                    taskType = 1,
                    className = "StartShoot"
                },
                new BehaviorData.TaskData
                {
                    id = 19,
                    taskType = 1,
                    className = "UpdateShoot"
                },
                new BehaviorData.TaskData
                {
                    id = 20,
                    taskType = 1,
                    className = "SearchTarget"
                },
                new BehaviorData.TaskData
                {
                    id = 21,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 22,
                    taskType = 1,
                    className = "IsShooting"
                },
                new BehaviorData.TaskData
                {
                    id = 23,
                    taskType = 1,
                    className = "StopShoot"
                },
                new BehaviorData.TaskData
                {
                    id = 24,
                    taskType = 2,
                    className = "Selector"
                },
                new BehaviorData.TaskData
                {
                    id = 25,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 26,
                    taskType = 1,
                    className = "CanMoveToTarget"
                },
                new BehaviorData.TaskData
                {
                    id = 27,
                    taskType = 1,
                    className = "MoveToTarget"
                },
                new BehaviorData.TaskData
                {
                    id = 28,
                    taskType = 1,
                    className = "IsTargetOutOfMaxRange"
                },
                new BehaviorData.TaskData
                {
                    id = 29,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 30,
                    taskType = 1,
                    className = "IsShooting"
                },
                new BehaviorData.TaskData
                {
                    id = 31,
                    taskType = 3,
                    className = "Inverter"
                },
                new BehaviorData.TaskData
                {
                    id = 32,
                    taskType = 1,
                    className = "IsTargetOutOfMaxRange"
                },
                new BehaviorData.TaskData
                {
                    id = 33,
                    taskType = 2,
                    className = "Selector"
                },
            };
            return data;
        }

        private static Dictionary<string,string> langMap = new Dictionary<string, string>();

        public static void OnLangChange()
        {
            langMap.Clear();
        }
        
        public static string GetLangText(string key)
        {
            if (langMap.TryGetValue(key, out var content))
            {
                return content;
            }
            var lang = ResidentHandler.Inst.GetFunction("L_SurvivorManager.GetLangText").Call(key)[0] as string;
            langMap.Add(key,lang);
            return lang;
        }
        
        public static BehaviorData GetDefaultBehaviorData()
        {
            var data = new BehaviorData();
            data.rootId = 1;
            data.composites = new Dictionary<int, int[]>();
            data.decorators = new Dictionary<int, int>();
            data.sharedVariables = new Dictionary<string, object>();

            data.composites.Add(1,new []{2,7,13});
            data.composites.Add(2,new []{3,4});
            data.composites.Add(4,new []{5,6});
            data.composites.Add(7,new []{8,9,12});
            data.composites.Add(9,new []{10,11});
            data.tasks = new[]
            {
                new BehaviorData.TaskData
                {
                    id = 1,
                    taskType = 2,
                    className = "Selector"
                },
                new BehaviorData.TaskData
                {
                    id = 2,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 3,
                    taskType = 1,
                    className = "NeedReload"
                },
                new BehaviorData.TaskData
                {
                    id = 4,
                    taskType = 2,
                    className = "Selector"
                },
                new BehaviorData.TaskData
                {
                    id = 5,
                    taskType = 1,
                    className = "IsReloading"
                },
                new BehaviorData.TaskData
                {
                    id = 6,
                    taskType = 1,
                    className = "Reload"
                },
                new BehaviorData.TaskData
                {
                    id = 7,
                    taskType = 2,
                    className = "Sequence"
                },
                new BehaviorData.TaskData
                {
                    id = 8,
                    taskType = 1,
                    className = "HasTarget"
                },
                new BehaviorData.TaskData
                {
                    id = 9,
                    taskType = 2,
                    className = "Selector"
                },
                new BehaviorData.TaskData
                {
                    id = 10,
                    taskType = 1,
                    className = "IsShooting"
                },
                new BehaviorData.TaskData
                {
                    id = 11,
                    taskType = 1,
                    className = "StartShoot"
                },
                new BehaviorData.TaskData
                {
                    id = 12,
                    taskType = 1,
                    className = "UpdateShoot"
                },
                new BehaviorData.TaskData
                {
                    id = 13,
                    taskType = 1,
                    className = "SearchTarget"
                },
            };
            return data;
        }
        
    }
}
